⬅️ Previous: Master Selenium Python➡️ Next: Chapter 3 – Selenium WebDriver Core Concepts

📘 Chapter 2: Selenium with Python – Fundamentals


🌟 2.1 What is Selenium?

Selenium is an open-source automation tool used for testing web applications across different browsers and platforms. It mimics real user actions like clicking buttons, typing in forms, navigating links, etc.

✅ Why Selenium?

🧠 2.2 Selenium Architecture Overview

Selenium has four main components:

✅ 1. Selenium IDE

🛠 What is it?
Selenium IDE is a browser extension (for Chrome and Firefox) that allows recording, editing, and debugging of functional tests.

❌ 2. Selenium RC (Remote Control)

🛠 What is it?
Selenium RC was the original solution for automating browsers before WebDriver.

✅ Why deprecated: Required server setup, slower, replaced by WebDriver.

🚀 3. Selenium WebDriver – Modern Standard

Allows direct control of browsers through language-specific bindings.

[Test Script]
↓
[Selenium Client Library (Python)]
↓
[Browser Driver (e.g., ChromeDriver)]
↓
[Browser (e.g., Chrome)]

🌐 4. Selenium Grid

Allows parallel test execution across machines and browsers. Useful for CI/CD.

🛠️ 2.3 Installation & Environment Setup

✅ Step 1: Install Python
Download from https://python.org and ensure “Add Python to PATH” is checked.

python --version

✅ Step 2: Install Selenium Library

pip install selenium

🧪 2.4 First Selenium Test Script in Python

✅ Test Case: Open Google and search for "Selenium"

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
driver.get("https://www.google.com")

search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("Selenium")
search_box.submit()

time.sleep(5)
driver.quit()

🔍 2.5 Locators in Selenium

Locators help Selenium find elements on a webpage.

Common Locator Strategies:

✅ Examples of Each Locator

# ID
driver.find_element(By.ID, "username").send_keys("admin")

# Name
driver.find_element(By.NAME, "password").send_keys("admin123")

# Class Name
driver.find_element(By.CLASS_NAME, "login-button").click()

# Tag Name
all_links = driver.find_elements(By.TAG_NAME, "a")
for link in all_links:
    print(link.text)

# Link Text
driver.find_element(By.LINK_TEXT, "Forgot Password?").click()

# Partial Link Text
driver.find_element(By.PARTIAL_LINK_TEXT, "Forgot").click()

🤔 Why use XPath?

🧠 Types of XPath

1️⃣ Absolute XPath ❌

It starts from the root of the HTML document and goes through each node until the desired element.

/html/body/div[1]/form/input[2] – Brittle, not recommended

2️⃣ Relative XPath ✅

Uses double forward slashes to search anywhere in the document.

//tagname[@attribute='value']

Example:

driver.find_element(By.XPATH, "//input[@id='username']")

🔍 XPath Syntax Variants

//input[@name='email']
//input[contains(@name, 'user')]
//input[starts-with(@id, 'user')]
//button[text()='Submit']
//a[contains(text(), 'Login')]

🔗 XPath Axes

//input[@id='email']/parent::div
//label[text()='Username']/following-sibling::input
//input[@id='password']/preceding-sibling::label
//span[text()='Login']/ancestor::form

🛠 Real World Example

<div class="form">
  <label for="email">Email:</label>
  <input type="text" name="email" id="emailInput" />
</div>

Example 1:

driver.find_element(By.XPATH, "//input[@id='emailInput']")

Example 2:

driver.find_element(By.XPATH, "//label[text()='Email:']/following-sibling::input")

🚨 Common Mistakes

✅ Best Practices

✅ XPath Summary Table

SyntaxDescriptionExample
//tag[@attr='value']Basic selector//input[@id='user']
contains()Partial match//a[contains(text(),'Sign')]
starts-with()Start of attr//input[starts-with(@id, 'user')]
text()Match text//button[text()='Login']
parent::, ancestor::Traverse up//input/parent::div
following-sibling::Select sibling//label/following-sibling::input

📌 Sample Project Folder Structure

selenium-python-demo/
├── chromedriver.exe
├── first_test.py
└── requirements.txt

✅ requirements.txt

Install all dependencies:

pip install -r requirements.txt

Example contents:

selenium==4.18.1
pytest
requests

✅ Final Tip

To create requirements.txt:

pip freeze > requirements.txt
⬅️ Previous: 02-Chapter 2 Selenium With Python Fundamentals ➡️ Next: Chapter 3 – Selenium WebDriver Core Concepts">➡️ Next: Chapter 3 – Selenium WebDriver Core Concepts